//
//  UIBarButtonItem+Extension.swift
//  CYWeibo
//
//  Created by 媛 on 16/5/20.
//  Copyright © 2016年 yuan. All rights reserved.
//

import UIKit

extension UIBarButtonItem {
    
    //扩展自定义的构造方法
    //给函数的参数设置默认值
    //函数的参数的默认值 如果不传递 就直接使用默认值
    convenience init(imageName: String? = nil, title: String? = nil, target: AnyObject?, action:Selector) {
        //便利函数里不能super
        self.init()
        
        let btn = UIButton()
        
        //设置图片
        if let img = imageName {
            btn.setImage(UIImage(named: img), forState: .Normal)
            btn.setImage(UIImage(named: img + "_highlighted"), forState: .Highlighted)
        }
        btn.addTarget(target, action: action, forControlEvents: .TouchUpInside)
        btn.setTitle(title, forState: .Normal)
        //设置图片的现实范围
        btn.imageEdgeInsets = UIEdgeInsets(top: 0, left: -8, bottom: 0, right: 0)
        
        //设置颜色
        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
        
        //设置自适应大小
        btn.sizeToFit()
        //设置自定义视图
        self.customView = btn
        
    }
}
